package cn.msqweb.src.controller;

import cn.msqweb.entity.Order;
import cn.msqweb.entity.ProdComm;
import cn.msqweb.model.Result;
import cn.msqweb.src.service.OrderService;
import cn.msqweb.src.vo.OrderStatusCount;
import cn.msqweb.util.AuthUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import jakarta.annotation.Resource;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/*
商出用户购物
 */
@Api("商出用户购物")
@RestController
@RequestMapping("p/myOrder")
public class OrderController {

    @Resource
    private OrderService orderService;

    @ApiOperation("查询会员订单状态数量对象")
    @GetMapping("orderCount")
    public Result<OrderStatusCount> loadMemberOrderStatusCount(){
        OrderStatusCount orderStatusCount = orderService.selectOrderStatusByOpenId(AuthUtils.getLoginMemberOpenId());
        return Result.success(orderStatusCount);
    }

//    p/myOrder/myOrder?current=1&size=5&status=1

    @ApiOperation("分页查询会员订单列表")
    @GetMapping("myOrder")
    public Result<Page<Order>> loadMemberOrderPage(@ApiParam("页码") @RequestParam Long current,
                                                   @ApiParam("每页显示条数") @RequestParam Long size,
                                                   @ApiParam("订单状态") @RequestParam Integer status){
        Page<Order> page = orderService.queryMemberOrderPageByOpenId(current,size,status);
        return Result.success(page);
    }


    @ApiOperation("根据订单编号查询订单详情")
    @GetMapping("orderDetail")
    public Result<Order> loadWxOrderDetail(@ApiParam("订单编号") @RequestParam String orderNumber) {
        Order order = orderService.queryWxOrderDetailByOrderNumber(orderNumber);
        return Result.success(order);
    }

//    receipt/1544227267721433088
    @ApiOperation("会员确认收货")
    @PutMapping("receipt/{orderNumber}")
    public Result<String> receiptMemberOrder(@ApiParam("订单编号") @PathVariable String orderNumber){
        Boolean result = orderService.receiptMemberOrder(orderNumber);
        return Result.handler(result);
    }

    @ApiOperation("会员删除订单")
    @DeleteMapping("{orderNumber}")
    public Result<String> removeMemberOrder(@ApiParam("订单编号") @PathVariable String orderNumber){
        Boolean result = orderService.removeMemberOrderByOrderNumber(orderNumber);
        return Result.handler(result);
    }

    @ApiOperation("会员取消订单")
    @PutMapping("cancel/{orderNumber}")
    public Result<String> cancelMemberOrder(@ApiParam("订单编号") @PathVariable String orderNumber) {
        Boolean canceled = orderService.cancelMemberOrder(orderNumber);
        return Result.handler(canceled);
    }
}
